<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\LiveWemeModel;
use Common\Model\LiveFriendCircleModel;
use Common\Model\LiveWemeNicknameModel;
//use ApiInterfaceController;
//include 'ApiInterfacecontroller.class.php';

/**
 * Description of WemeController
 *
 * @author
 */
class WemeController extends AdminbaseController {

    function _initialize() {

        parent::_initialize();
        $this->_mod = D('LiveWeme');
        $this->assign('status', [100 => '未发送', 1 => '已发送到图文直播', 2 => '已发送到汇友圈', 3 => '已发送到图文直播和汇有圈']);
        
        $this->user_admin_model = D("Common/AdminUser");
        $admin_id = $_SESSION["ADMIN_ID"];
        $this->company_id = $this->user_admin_model->where(array('admin_id' => $admin_id))->getField('company_id');
        $this->list_live_id=D('Common/Live')->where(array('company_id' => $this->company_id,"category"=>7))->select();
    }

    function index() {
        $map = $this->_search();
        $mod = $this->_mod;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
            $this->assign('page', $result['page']);
            $list = $result['list'];
            foreach ($list as $key => $val) {
            $list[$key]['nickname']=D('LiveWemeNickname')->where(array('mirrtalkID'=>$val['mirrtalkid']))->getField('nickname');

       }
            $this->assign('list', $list);
            $this->display();
        }
    }


    public function send_weme() {
        if ($this->_request('action')) {
                if ($this->_request('type')==1) {
                        $id=$this->_request('id');
                        !$this->_request('cover')&&$this->ajaxReturn(array("status" => 0, "info" => "未上传图片！"));
                        $weme= $this->_mod->where(array('id'=>$id))->find();
                        $weme_nickname=D('LiveWemeNickname')->where(array('mirrtalkID'=>$weme['mirrtalkid']))->find();
                        !$weme_nickname['nickname']&&$this->ajaxReturn(array("status" => 0, "info" => "昵称不能为空"));
                        $data['author'] =$weme_nickname['nickname'];
                        $data['avatar'] =$weme_nickname['url'];
                        $data['authorid'] =$weme_nickname['id'];
                        $data['subject'] =$this->_request('content');
                        $data['dateline'] = time();
                        $data['status'] = 32;
                        $data['url'] =$weme['url'];
                        $data['icon'] = $this->_request('cover');
                        foreach ($this->list_live_id as $key => $value) {
                            $data['live_id'] =$value['live_id'];
                            $res=D('LiveFriendCircle')->add($data);
                        }
                        
                        if($weme['status']==0){
                            $this->_mod->where(array('id'=>$id))->save(array('status'=>2));
                        }
                         if($weme['status']==1){
                            $this->_mod->where(array('id'=>$id))->save(array('status'=>3));
                        }
                        $res ? $this->ajaxReturn(array("status" => 1, "info" => "发布成功")) : $this->ajaxReturn(array("status" => 0, "info" => "发布失败！"));
                        return;
                }
                
                if ($this->_request('type')==6) {
                        $id=$this->_request('id');
                        $gallery= D('LiveGallery')->where(array('id'=>$id))->find();
                        $weme_nickname=D('LiveWemeNickname')->where(array('id'=>rand(2, 15)))->find();
                        $data['author'] =$gallery['nickname'];
                        $data['avatar'] =$weme_nickname['url'];
                        $data['authorid'] =$weme_nickname['id'];
                        $data['subject'] =$this->_request('content');
                        $data['dateline'] = time();
                        $data['status'] = 32;
                        $data['url'] =$gallery['url'];
                        $res=D('LiveFriendCircle')->add($data);
                        if($gallery['status']==0){
                            D('LiveGallery')->where(array('id'=>$id))->save(array('status'=>2));
                        }
                         if($gallery['status']==1){
                            D('LiveGallery')->where(array('id'=>$id))->save(array('status'=>3));
                        }
                        $res ? $this->ajaxReturn(array("status" => 1, "info" => "发布成功")) : $this->ajaxReturn(array("status" => 0, "info" => "发布失败！"));
                        return;
                }
                if ($this->_request('type')==0) {
                        $id=$this->_request('id');
                        $weme= $this->_mod->where(array('id'=>$id))->find();
                        if($weme['status']==0){
                            $this->_mod->where(array('id'=>$id))->save(array('status'=>1));
                        }
                        if($weme['status']==2){
                            $this->_mod->where(array('id'=>$id))->save(array('status'=>3));
                        }
                        return;
                }
                if ($this->_request('type')==3) {
                        $id=$this->_request('id');
                        $gallery= D('LiveGallery')->where(array('id'=>$id))->find();
                        if($gallery['status']==0){
                            D('LiveGallery')->where(array('id'=>$id))->save(array('status'=>1));
                        }
                         if($gallery['status']==2){
                            D('LiveGallery')->where(array('id'=>$id))->save(array('status'=>3));
                        }
                        return;
                }
                
        }
        if ($this->_request('type')==0) {
            $id=$this->_request('id');
            $weme= $this->_mod->where(array('id'=>$id))->find();
            $this->assign('videos', $weme['url']);
        }
        if ($this->_request('type')==3) {
            $id=$this->_request('id');
            $gallery= D('LiveGallery')->where(array('id'=>$id))->find();
            $this->assign('files', $gallery['url']);
        }
        $this->assign('id', $this->_request('id'));
        $this->assign('type', $this->_request('type'));
        $this->display();
    }


    public function send_list() {
        $where['status'] = array('exp', ' IN (1,3) ');
        $where['category'] = array('exp', ' IN (3,4,5) ');
        $live_list = D('Live')->where($where)->select();
        echo json_encode($live_list);
    }

    protected function _list($model, $map = array(), $sort_by = '', $order_by = '', $field_list = '*', $pagesize = 20) {
        //排序
        $mod_pk = $model->getPk();
        if ($this->_request("sort", 'trim')) {
            $sort = $this->_request("sort", 'trim');
        } else if (!empty($sort_by)) {
            $sort = $sort_by;
        } else if ($this->sort) {
            $sort = $this->sort;
        } else {
            $sort = $mod_pk;
        }
        if ($this->_request("order", 'trim')) {
            $order = $this->_request("order", 'trim');
        } else if (!empty($order_by)) {
            $order = $order_by;
        } else if ($this->order) {
            $order = $this->order;
        } else {
            $order = 'DESC';
        }
        $result = array();
        //如果需要分页
        if ($pagesize) {
            $count = $model->where($map)->count($mod_pk);
            $pager = $this->page($count, $pagesize);
        }
        $select = $model->field($field_list)->where($map)->order($sort . ' ' . $order);
        $this->list_relation && $select->relation(true);
        if ($pagesize) {
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show('Admin');
            $result['page'] = $page;
            $this->assign("page", $page);
        }
        $list = $select->select();
        $result['list'] = $list;
        $this->assign('list_table', true);
        return $result;
    }

    protected function _search() {
      $map = array();
        ($author = $this->_request('author', 'trim')) && $map['mirrtalkID'] = array('IN', $this->_get_uids($author, 'nickname'));
         if ($this->_request('status')) {
            $status = ($this->_request('status') != 100) ? $this->_request('status') : 0;
            $map['status'] = $status;
        }
        ($time_start = $this->_request('stime', 'trim')) && $map['mtime'] = array(array('egt', strtotime($time_start)), array('lt', strtotime($this->_request('etime', 'trim'))));
        $this->assign('search', array(
            'author' => $this->_request('author'),
            'stime' => $this->_request('stime'),
            'etime' => $this->_request('etime'),
            'status' => $this->_request('status'),
        ));
        return $map;
    }

    protected function _request($parama) {
        return I($parama);
    }

    protected function _get_uids($keyword, $field) {
    $where[$field] = array('like', '%' . $keyword . '%');
    $uidarr = D('LiveWemeNickname')->where($where)->field('mirrtalkid')->select();
    $_idarr = array();
    foreach ($uidarr as $v) {
        $_idarr[] = $v['mirrtalkid'];
    }
    return implode(',', $_idarr);
    }

}
